package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public OverviewVo getOverview() {
        // 1.查询Redis缓存中的数据
        log.info("从Redis中，查询线索和商机的数据概览");
        Object object = redisTemplate.opsForValue().get("OVERVIEW_DATA");
        if (object != null) {
            return (OverviewVo) object;
        }

        log.info("从数据库中，查询线索和商机的数据概览");
        // 2.1 获取线索线索对应的数据
        OverviewVo clueOverview =clueMapper.getOverview();
        // 2.2 获取商机对应的数据
        OverviewVo businessOverview = businessMapper.getOverview();
        // 2.3 合并数据,封装到OverviewVo
        BeanUtils.copyProperties(clueOverview,businessOverview,"businessTotal","businessWaitAllot","businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");
        // 3. 保存数据到Redis缓存中
        log.info("将数据保存到Redis缓存中");
        redisTemplate.opsForValue().set("OVERVIEW_DATA",businessOverview, 1, TimeUnit.MINUTES);
        return businessOverview;
    }
}
